import router from './router'
import store from './store'
import { getUserId } from '@/utils/auth'

// const whiteList = ['/', '/UserCenter/index', '/login/index','/forgetPwd/index']
const whiteList = ['/login/index', '/learnLogin/index', '/register/index']
router.beforeEach((to, from , next) => {
    store.commit('updateLoadingStatus', {isLoading: true})
    if(getUserId()) {
        // 如果有userId
        if(to.path === '/login/index') {
            next({ path: '/' })
            store.commit('updateLoadingStatus', {isLoading: false})
        } else {
            next()
        }
        // else {
        //     store.dispatch('getUserInfo').then(res => {
        //         next()
        //     }).catch(err => {
        //         console.log('permission.js error')
        //     })
        // }
    } else {
        if(whiteList.indexOf(to.path) !== -1) {
            next();
        } else {
            next(`/login?redirect=${to.path}`);
        }
    }
        
})
router.afterEach((to) => {
    store.commit('updateLoadingStatus', {isLoading: false})
})